package com.jiantong.news.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.news.impl.dao.ThinktankDao;
import com.jiantong.news.model.Thinktank;
import com.jiantong.news.service.ThinktankReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 养殖智库
 * Created by mz03 on 2017/4/13.
 */
@Slf4j
@Service
@RpcProvider
public class ThinktankReadServiceImpl implements ThinktankReadService {

    private final ThinktankDao thinktankDao;

    @Autowired
    public ThinktankReadServiceImpl(ThinktankDao thinktankDao) {
        this.thinktankDao = thinktankDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return Thinktank
     */
    @Override
    public Response<Thinktank> finById(Long id) {
        try{
            Thinktank thinktank = thinktankDao.findById(id);
            if(thinktank == null){
                //判断根据id查出来的结果,判断其如果是空值 抛异常信息
                log.error("thinktank (id={}) is not found", id);
                return Response.fail("thinktank.not.found");
            }
            return Response.ok(thinktank);
        } catch (Exception e) {
            log.error("failed to find thinktank(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("thinktank.find.fail");
        }

    }

    /**
     * 根据类型type 查找
     *
     * @param type
     * @return List<Thinktank>
     */
    @Override
    public Response<List<Thinktank>> finByType(Integer type) {
        try{
            List<Thinktank> list = thinktankDao.findByType(type);
            if (list == null){
                log.error("thinktanks (type = {}) is not found",type);
                return Response.fail("thinktanks.not.found");
            }
            return Response.ok(list);
        }catch (Exception e){
            log.error("failed to find thinktanks(type = {}), cause:{}",type,Throwables.getStackTraceAsString(e));
            return Response.fail("thinktanks.find.fail");
        }
    }

    /**
     * 根据类型分页查询
     *
     * @param pageNo   页码
     * @param pageSize 条数
     * @param types    类型
     * @return Paging<Thinktank>
     */
    @Override
    public Response<Paging<Thinktank>> pagingThinktank(Integer pageNo, Integer pageSize, List<Integer> types) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(types != null){
                criteria.put("types",types);
            }
            criteria.put("status",1);
            return Response.ok(thinktankDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find thinktanks (types={}), cause:{}", types, Throwables.getStackTraceAsString(e));
            return Response.fail("thinktanks.find.fail");
        }
    }

    /**
     * 根据关键字匹配内容搜索 分页
     *
     * @param pageNo
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Response<Paging<Thinktank>> pagingThinktankKeyword(Integer pageNo, Integer pageSize, String keyword,Integer type) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(keyword != null){
                criteria.put("keyword",keyword);
            }
            if(type != null){
                criteria.put("type",type);
            }
            criteria.put("status",1);
            return Response.ok(thinktankDao.pagingByKeyword(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find thinktanks (keyword={}), cause:{}", keyword, Throwables.getStackTraceAsString(e));
            return Response.fail("thinktanks.find.fail");
        }
    }

    @Override
    public Response<Paging<Thinktank>> pagingWeb(Integer pageNo, Integer pageSize, Integer type) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo , pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(type != null){
                criteria.put("type",type);
            }
            return Response.ok(thinktankDao.pagingWeb(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to find thinktanks (type={}), cause:{}", type, Throwables.getStackTraceAsString(e));
            return Response.fail("thinktanks.find.fail");
        }
    }
}
